package com.dnxh.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnxh.constant.CustomResult;
import com.dnxh.entity.BasicMeeting;
import com.dnxh.entity.Meeting;
import com.dnxh.entity.SearchBasicMeetingVo;
import com.dnxh.entity.SendMeetingEmailVo;
import com.dnxh.entity.common.PageContext;
import com.dnxh.entity.request.SearchBasicMeetingRequest;
import com.dnxh.entity.request.SetLocationRequest;
import com.dnxh.mapper.BasicMeetingMapper;
import com.dnxh.service.BasicMeetingService;
import com.dnxh.service.MeetingService;
import com.dnxh.util.EmailUtil;
import com.dnxh.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhiqiu
 * @since 2022-04-13
 */
@Slf4j
@Service
public class BasicMeetingServiceImpl extends ServiceImpl<BasicMeetingMapper, BasicMeeting> implements BasicMeetingService {
	
	@Resource
	private              BasicMeetingMapper basicMeetingMapper;
	@Value ("${server.port}")
	private              int                port;
	/**
	 * 签到二维码
	 */
	private static final String             CHECK_IN_QR = "/common/email/qr?data=";
	/**
	 * 发送邮件
	 */
	@Resource
	private EmailUtil emailUtil;
	
	@Override
	public CustomResult search (SearchBasicMeetingRequest request) {
		IPage<SearchBasicMeetingVo> iPage = new Page<SearchBasicMeetingVo> ()
				.setSize (request.getPageSize ())
				.setCurrent (request.getPageNum ());
		List<SearchBasicMeetingVo> list = basicMeetingMapper.search (iPage, request.getName (),
				request.getExistLocation (), request.getLocation ());
		return CustomResult.success (new PageContext<SearchBasicMeetingVo> ()
				.setList (list)
				.setCount (iPage.getTotal ()));
	}
	
	@Override
	public CustomResult setLocation (SetLocationRequest request) {
		update (Wrappers.<BasicMeeting>lambdaUpdate ()
				.set (BasicMeeting::getLocation, request.getLocation ())
				.in (BasicMeeting::getId, request.getId ()));
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult del (Long id) {
		basicMeetingMapper.del (id);
		return CustomResult.success ();
	}
	
	@Override
	public CustomResult sendMeetingEmail (Long id) {
		if (count (Wrappers.<BasicMeeting>lambdaQuery ()
				.eq (BasicMeeting::getMeetingId, id)
				.isNull (BasicMeeting::getLocation)) > 0) {
			return CustomResult.fail ("存在未分配地址人员，请核对后重新处理!");
		} else {
			List<SendMeetingEmailVo> list = basicMeetingMapper.searchMeetingEmail (id);
			if (CollUtil.isEmpty (list)) {
				return CustomResult.fail ("需发送的人员为空!");
			}
			emailUtil.sendEmail (list);
		}
		return CustomResult.success ();
	}
}
